package tx.bj12320.process;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.activiti.engine.TaskService;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.TaskListener;
import org.activiti.engine.impl.persistence.entity.ExecutionEntity;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import tx.bj12320.model.ActivityInfo;
import tx.bj12320.model.TszbProcessInfo;
import tx.bj12320.model.enums.ActivityEnum;
import tx.bj12320.model.enums.ActivitySysUserTypeEnum;
import tx.bj12320.model.enums.WorkOrderTypeEnum;
import tx.bj12320.service.ActivityInfoManager;
import tx.bj12320.service.TszbProcessInfoManager;
import tx.bj12320.util.WebUtils;
@Service
@Transactional
public class TszbProcessTaskHandler implements TaskListener {
	protected Log log = LogFactory.getLog(getClass());
	@Autowired
	private ActivityInfoManager activityInfoManager;
	public void setActivityInfoManager(ActivityInfoManager manager) {
		this.activityInfoManager = manager;
	}
	@Autowired
	private TszbProcessInfoManager tszbProcessInfoManager;
	public void setTszbProcessInfoManager(TszbProcessInfoManager manager) {
		this.tszbProcessInfoManager = manager;
	}
	@Autowired
	private TaskService taskService;
	public void setTaskService(TaskService taskService) {
		this.taskService = taskService;
	}

	public void notify(DelegateTask delegateTask) {

	}

	/**
	 * 通过监听器自动完成UserTask报错，通过修改流程图来实现，
	 * @deprecated
	 * @param delegateTask
	 */
	private void noUse(DelegateTask delegateTask) {
		TszbProcessInfo tszbProcessInfo = tszbProcessInfoManager.getById(Long
				.valueOf(delegateTask.getExecution().getProcessBusinessKey()));
		// 建议、表扬和未形成告知单不用中心领导审核。
		if (delegateTask.getTaskDefinitionKey().equals(
				ActivityEnum.zgzrsd.getCode())) {
			if (tszbProcessInfo.getWorkOrderType().equals(
					WorkOrderTypeEnum.JYSLD.getCode())
					|| tszbProcessInfo.getWorkOrderType().equals(
							WorkOrderTypeEnum.BYSLD.getCode())
					|| tszbProcessInfo.getWorkOrderType().equals(
							WorkOrderTypeEnum.TSWXCGZD.getCode())) {
				ActivityInfo activityInfo = new ActivityInfo();
				activityInfo.setTszbProcessInfoId(tszbProcessInfo
						.getTszbProcessInfoId());
				activityInfo.setUserInfoId(WebUtils.getUserInSession()
						.getUserInfoId());
				activityInfo.setOperatorName(WebUtils.getUserInSession()
						.getRealName());
				activityInfo.setReceiveTime(delegateTask.getCreateTime());
				activityInfo.setFinishTime(new Date());
				activityInfo.setActivityName(delegateTask
						.getTaskDefinitionKey());
				if (!WebUtils.isNullOrEmptyString(activityInfo
						.getIncomingCallContent())) {
					activityInfo.setIncomingCallContentBak(tszbProcessInfo
							.getIncomingCallContent());
					tszbProcessInfo.setIncomingCallContent(activityInfo
							.getIncomingCallContent());
				}
				activityInfoManager.save(activityInfo);
				taskService.claim(delegateTask.getId(), "1");//由超级管理员自动办理完成
				Map<String, Object> vars = new HashMap<String, Object>();
				vars.put("isPass", true);
				taskService.complete(delegateTask.getId(), vars);
				tszbProcessInfo.setCurrActName(ActivityEnum.xtdbff.getCode());
				tszbProcessInfo.setCurrSysUserType("BJ12320");
				tszbProcessInfoManager.saveOrUpdate(tszbProcessInfo);
				log.info(tszbProcessInfo.getWorkOrderType());
			}
		}
	}
}
